
/**
 * @file	queue.cpp
 * @author  Alessandro Carrega <contact@alexcarrega.com>
 * @version 2.0
 *
 * @secion LICENSE
 *
 * This file is part of graph_max_flow.
 *
 * graph_max_flow is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * graph_max_flow is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with graph_max_flow.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

#include <cstring>

#include "queue.h"

Queue::Queue(int num_node): num_node_(num_node)
{
	/*
	 * Alloco la memoria per la coda a dimensione fissa.
	 */	
	q_ = new int[num_node + 2];
	// Alloco il vettore dei visitati indicizzato dai nodi (prima posizione non usata).
	visited_ = new uint8_t[num_node + 1];
}

void Queue::reset()
{
	// Svuoto la coda.
	head_ = tail_ = 0;
	// Azzero le visite dei nodi. La prima posizione non serve dato che i nodi partono da 1.
	memset(visited_ + 1, TO_VISIT, num_node_);
}

void Queue::enqueue(int u) 
{
	// Inserisco in coda e aggiorno lo stato del nodo.
	visited_[u] = IN_QUEUE;
	q_[tail_] = u;
	tail_++;
}

int Queue::dequeue()
{
	// Estraggo dalla testa della coda e aggiorno lo stato del nodo.
	int u = q_[head_++];
	visited_[u] = VISITED;
	return u;
}

bool Queue::empty()
{
	return head_ == tail_;
}

bool Queue::is_to_visit(int u)
{
	return visited_[u] == TO_VISIT;
}

bool Queue::is_in_queue(int u)
{
	return visited_[u] == IN_QUEUE;
}

bool Queue::is_visited(int u)
{
	return visited_[u] == VISITED;
}
